<template>
  <div>
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }"
          ><i class="el-icon-s-home"></i> 首页</el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><i class="el-icon-share"></i> 文章管理</el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><i class="el-icon-share"></i> 文章列表</el-breadcrumb-item
        >
      </el-breadcrumb>
      <el-row type="flex" justify="end" style="margin: 20px 0">
        <el-col :span="22"
          ><el-form :inline="true" class="demo-form-inline">
            <el-form-item label="文章分类">
              <el-select
                placeholder="请选择分类"
                size="small"
                v-model="query.cate_id"
              >
                <el-option
                  v-for="item in cateList"
                  :key="item.id"
                  :label="item.cate_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发布状态">
              <el-select
                placeholder="请选择状态"
                size="small"
                v-model="query.state"
              >
                <el-option label="已发布" value="已发布"></el-option>
                <el-option label="草稿" value="草稿"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="getArtList"
                >查询</el-button
              >
              <el-button size="small" type="primary" @click="result"
                >重置</el-button
              >
            </el-form-item>
          </el-form></el-col
        >
        <el-col :span="2"
          ><el-button size="small" type="primary" @click="addTextCate"
            >发表文章</el-button
          ></el-col
        >
      </el-row>
      <!-- {{cateList}} -->
      <!-- 文章列表表格 -->
      <el-table :data="artlist" border stripe>
        <el-table-column label="文章标题">
          <template #default="{ row }">
            <el-link type="primary" @click="getArtInfo(row.id)">{{
              row.title
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="分类"></el-table-column>
        <el-table-column label="发表时间">
          <template #default="{ row }">
            {{ row.pub_date | formDateTime }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            {{ row.id }}
            <el-button type="danger" size="small" @click="decArtCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 预览文章 -->
      <el-dialog
        title="提示"
        top="15vh"
        :visible.sync="showDialogVisible"
        width="70%"
      >
        <!-- 文章内容组件 抽离子组件 -->
        <ArtInfo :artInfo="artInfo"></ArtInfo>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="showDialogVisible = false"
            >关 闭</el-button
          >
        </span>
      </el-dialog>
      <!-- 发表文章的对话框 -->
      <el-dialog
        title="发表文章"
        destroy-on-close
        :visible.sync="dialogVisible"
        fullscreen
        :before-close="closePublist"
      >
      <!-- 新增文章的内容 -->
        <ArtPub @closeDialog="pubSuccess"></ArtPub>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { reqGetArtInfo, reqGetArtList, reqDelArticleById } from '@/api/article'
import { mapState } from 'vuex'
import ArtInfo from './components/ArtInfo.vue'
import ArtPub from './components/ArtPub.vue'
export default {
  name: 'ListPage',
  components: {
    ArtInfo,
    ArtPub
  },
  data () {
    return {
      value: '',
      query: {
        pagenum: 1, // 页码
        pagesize: 2, // 每页条数
        cate_id: '', // 查询文章分类
        state: '' // 查询文章状态
      },
      artlist: [], // 文章列表
      total: 0, // 总数
      showDialogVisible: false,
      artInfo: {}, // 查询的文章信息
      dialogVisible: false // 添加表单
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  created () {
    this.getArtList()
    // 获取文章分类 渲染下拉表单
    this.$store.dispatch('article/GetCateList')
  },
  methods: {
    // 获取文章列表
    async getArtList () {
      const { data } = await reqGetArtList(this.query)
      // console.log(res)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success('已为您重新刷新列表')
      this.artlist = data.data
      this.total = data.total
      this.pagenum = 1
    },
    // 重置
    result () {
      this.query.cate_id = ''
      this.query.state = ''
      this.pagenum = 1
      this.getArtList()
    },
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.query.pagesize = newSize
      this.getArtList()
    },
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.query.pagenum = newPage
      this.getArtList()
    },
    // 根据id获取文章
    async getArtInfo (id) {
      this.showDialogVisible = true
      const { data } = await reqGetArtInfo(id)
      // console.log(res)
      this.artInfo = data.data
    },
    // 删除功能
    async decArtCate (id) {
      // console.log(id)
      const { data } = await reqDelArticleById(id)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success('删除成功!')
      this.getArtList()
    },
    // 添加功能
    addTextCate () {
      this.dialogVisible = true
    },
    // 关闭文章发布提示
    async closePublist () {
      const str = await this.$confirm('此操作将导致文章信息丢失, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {
      })
      console.log(str)
      if (str !== 'confirm') return this.$message.success('您已取消退出')

      this.dialogVisible = false
    },
    pubSuccess () {
      this.dialogVisible = false
      this.getArtList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
  /deep/ .el-table__header-wrapper {
    .cell {
      color: #000 !important;
    }
  }
}
</style>
